import re

from common.BasePages import BasePage


class PointTablePage(BasePage):
    # 点表管理
    def point_table_url(self):
        self.goto_url("projectManagement/pointManagement")

    def create_point_table_tcp(self):
        # 创建网口
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("button", name="+").click()
        self.page.get_by_role("radio").click()
        self.page.get_by_role("button", name="确定").click()
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("button", name="添加点位").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").fill("点位新增33")
        self.page.get_by_role("textbox", name="进制数").click()
        self.page.get_by_role("textbox", name="进制数").fill("33")
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 AB$")).click()
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("button", name="确 定").click()
        self.page.get_by_role("button", name="保存", exact=True).click()
        self.assert_alert_content("保存成功")

    def create_point_table_rtu(self):
        # 创建串口
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("textbox", name="请选择协议类型").click()
        self.page.get_by_role("listitem").filter(has_text="串口").click()

        self.page.get_by_role("button", name="+").click()
        self.page.get_by_role("radio").click()
        self.page.get_by_role("button", name="确定").click()

        self.page.get_by_role("button", name="下一步").click()

        self.page.get_by_role("button", name="添加点位").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").fill("点位新增RTU-33")
        self.page.get_by_role("textbox", name="进制数").click()
        self.page.get_by_role("textbox", name="进制数").fill("33")
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 AB$")).click()
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("button", name="确 定").click()
        self.page.get_by_role("button", name="保存", exact=True).click()
        self.assert_alert_content("新增点位成功")

    def create_point_table_rtu_bool(self):
        # 创建串口
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("textbox", name="请选择协议类型").click()
        self.page.get_by_role("listitem").filter(has_text="串口").click()

        self.page.get_by_role("button", name="+").click()
        self.page.get_by_role("radio").click()
        self.page.get_by_role("button", name="确定").click()

        self.page.get_by_role("button", name="下一步").click()

        self.page.get_by_role("button", name="添加点位").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").fill("点位新增RTU-34")
        self.page.get_by_role("textbox", name="进制数").click()
        self.page.get_by_role("textbox", name="进制数").fill("34")
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("button", name="确 定").click()
        self.page.get_by_role("button", name="保存", exact=True).click()
        self.assert_alert_content("新增点位成功")

    def edit_point_table(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_12_column_171 > .cell > .basicTableBtnBox > .el-icon-edit-outline").first.click()
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("button", name="编辑").click()
        self.page.get_by_role("textbox", name="进制数").click()
        self.page.get_by_role("textbox", name="进制数").fill("34")
        self.page.get_by_role("button", name="确 定").click()
        self.page.get_by_role("button", name="保存", exact=True).click()
        self.assert_alert_content("保存成功")

    def delete_point_table(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_12_column_171 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确定").click()
        self.assert_alert_content("删除成功")
